package com.kmxd.ams.app.logs;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.vo.ArchiveTypeVO;
import com.kmxd.ams.client.logs.ILogArchiveSvc;
import com.kmxd.ams.client.logs.dto.LogArchiveAddDTO;
import com.kmxd.ams.client.logs.dto.LogArchiveQueryDTO;
import com.kmxd.ams.client.logs.vo.LogArchiveListVO;
import com.kmxd.ams.client.logs.vo.LogArchiveVO;
import com.kmxd.ams.core.common.constant.Constants;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.logs.entity.LogArchive;
import com.kmxd.ams.infra.logs.mapper.LogArchiveMapper;
import com.kmxd.ams.infra.tmpl.dto.ArchTmplBO;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 档案维护日志表 服务实现类
 *
 * @author ZuoLG
 * @since 2022-09-06
 */
@Service
@AllArgsConstructor
public class LogArchiveSvcImpl extends ServiceImpl<LogArchiveMapper, LogArchive>
    implements ILogArchiveSvc {

  private LogArchiveMapper logArchiveMapper;

  @Override
  public Page<LogArchiveListVO> selectPage(Page reqPage, LogArchiveQueryDTO req) {
    LambdaQueryWrapper<LogArchive> queryWrapper =
        QueryGen.init(new QueryWrapper<LogArchive>(), req).lambda();
    queryWrapper.eq(
        LogArchive::getComId,
        ObjectUtil.isNotEmpty(SecurityUtil.getCropId())
            ? SecurityUtil.getCropId()
            : Constants.DEFAULT_CROP_ID);
    Page<LogArchive> page = logArchiveMapper.selectPage(reqPage, queryWrapper);
    Page<LogArchiveListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), LogArchiveListVO.class));
    return voPage;
  }

  @Override
  public List<LogArchiveListVO> selectList(LogArchiveQueryDTO req) {
    LambdaQueryWrapper<LogArchive> queryWrapper =
        QueryGen.init(new QueryWrapper<LogArchive>(), req).lambda();
    queryWrapper.eq(
        LogArchive::getComId,
        ObjectUtil.isNotEmpty(SecurityUtil.getCropId())
            ? SecurityUtil.getCropId()
            : Constants.DEFAULT_CROP_ID);
    List<LogArchive> list = logArchiveMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, LogArchiveListVO.class);
  }

  @Override
  public List<LogArchiveListVO> selectList(LogArchiveQueryDTO req, String token) {
    LambdaQueryWrapper<LogArchive> queryWrapper =
        QueryGen.init(new QueryWrapper<LogArchive>(), req).lambda();
    queryWrapper.eq(
        LogArchive::getComId,
        ObjectUtil.isNotEmpty(SecurityUtil.getCropId(token))
            ? SecurityUtil.getCropId(token)
            : Constants.DEFAULT_CROP_ID);
    List<LogArchive> list = logArchiveMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, LogArchiveListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(LogArchiveAddDTO req) {
    LogArchive entity = BeanUtil.copyProperties(req, LogArchive.class);
    String tableId = entity.getTableId();
    if (StrUtil.isNotBlank(tableId)) {
      if (ObjectUtil.contains(tableId, ArchTmplBO.DOCUMENT)) {
        entity.setArchLevel(ArchTmplBO.DOCUMENT);
      }
      if (ObjectUtil.contains(tableId, ArchTmplBO.VOLUME)) {
        entity.setArchLevel(ArchTmplBO.VOLUME);
      }

      if (ObjectUtil.contains(tableId, ArchTmplBO.BOX)) {
        entity.setArchLevel(ArchTmplBO.BOX);
      }
      if (ObjectUtil.contains(tableId, ArchTmplBO.PROJECT)) {
        entity.setArchLevel(ArchTmplBO.PROJECT);
      }
    }
    logArchiveMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(LogArchiveAddDTO req) {
    LogArchive entity = BeanUtil.copyProperties(req, LogArchive.class);
    logArchiveMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public LogArchiveVO view(Long id) {
    LogArchive entity = logArchiveMapper.selectById(id);
    return BeanUtil.copyProperties(entity, LogArchiveVO.class);
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void saveArchiveLog(
      ArchiveTypeVO archiveTypeVO, String tableId, long docId, JSONArray logJsonArray) {
    if (ObjectUtil.isNotEmpty(logJsonArray)) {
      List<LogArchive> logList =
          logJsonArray.stream()
              .map(
                  log -> {
                    LogArchive vo = JSONUtil.parseObj(log).toBean(LogArchive.class);
                    vo.setArchId(docId);
                    vo.setArchTypeId(archiveTypeVO.getId());
                    vo.setTableId(tableId);
                    return vo;
                  })
              .collect(Collectors.toList());
      saveBatch(logList);
    }
  }
}
